﻿var Sys_RolePermissionList;
var Sys_RolePermissionStore;
// 功能模块grid展示
Sys_RolePermissionList = function() {
    var url = "/Manager/Sys_RolePermission/List/" + Sys_RolePermissionParentId;


    //grid 填充
    Ext.define('Sys_RolePermissionGridModel', {
        extend: 'Ext.data.Model',
        fields: [
            { name: "M_CName", type: "string" },
            { name: "M_Icon", type: "string" },
            { name: "M_PageCode", type: "string" },
            { name: "M_ParentName", type: "string" }
            ]
    });

    //grid获取 json数组
    Sys_RolePermissionStore = Ext.create('Ext.data.Store', {
        pageSize: 1000,
        model: 'Sys_RolePermissionGridModel',
        buffered: false,
        groupField: 'M_ParentName',
        proxy: {
            actionMethods: 'post',
            type: 'ajax',
            url: url,
            reader: {
                type: 'json',
                root: 'root',
                totalProperty: 'totalPorperty'
            }
        }
    });


    var groupingFeature = Ext.create('Ext.grid.feature.Grouping', {
        groupHeaderTpl: '权限组: {name} ({rows.length} Item{[values.rows.length > 1 ? "s" : ""]})'
    });
    var checked = false;
    var CheckSys_RolePermissionFn = function() {
        if (!checked) {
            selcheck();
            checked = true;
        }
        else {
            clearcheck();
            checked = false;
        }
    }
    function selcheck() {
        var objs = document.getElementsByTagName("input");
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].type.toLowerCase() == "checkbox")
                objs[i].checked = true;
        }
    }
    function clearcheck() {
        var objs = document.getElementsByTagName("input");
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].type.toLowerCase() == "checkbox")
                objs[i].checked = false;
        }
    }
    //新增信息
    SaveSys_RolePermissionFn = function() {

        var rowcount = grid.getStore().getCount();
        //console.log(rowcount);
        var reStr = "";
        for (i = 0; i < rowcount; i++) {
            reStr = reStr + grid.getStore().getAt(i).data.M_PageCode + ":";
            //console.log(grid.getStore().getAt(i));
            // console.log("rowcount" + i);
            var abc = grid.getView().getNode(i);
            // console.log(abc);
            var childs = abc.children[3].children[0];
            //console.log("length" + childs.children.length);
            //console.log(childs.innerHTML);
            var strValue = 0;
            var temp;
            for (var j = 0; j < childs.children.length; j++) {
                //reStr = reStr + childs.children[j].value + "_" + childs.children[j].checked;
                if (childs.children[j].checked) {
                    temp = childs.children[j].value.split("|");
                    if (temp.length > 1) {
                        strValue = strValue + parseInt(temp[1]);
                    }
                    console.log(temp);
                }
                if (j != (childs.children.length - 1)) {
                    //reStr = reStr + ","
                }
            }
            reStr = reStr + strValue;
            console.log(strValue);
            if (i != (rowcount - 1)) {
                reStr = reStr + ";"
            }
            //var cell = grid.getView().getCell(i, 3);
            //console.log(cell.outerText);

        }

        Ext.Ajax.request({
            url: "/Manager/Sys_RolePermission/Update/" + Sys_RolePermissionParentId,
            method: 'post',
            params: {
                Code: reStr
            },
            waitMsg: '正在提交角色信息...',
            success: function(d) {
                //成功后
                var action = Ext.JSON.decode(d.responseText);
                //alert(action.success);
                var flag = action.success;
                if (flag == "true") {

                    Ext.MessageBox.alert("恭喜", "保存成功!");
                }
                else {

                    if (action.result != undefined) {
                        Ext.MessageBox.alert("保存失败!", action.result.message);
                    }
                    else {
                        Ext.MessageBox.alert("提示!", "保存失败!");
                    }
                }
            },
            failure: function(form, action) {
                Ext.MessageBox.alert("提示!", "保存失败!");
            }
        })
        //Ext.Msg.confirm("内容", reStr);
        //console.log(reStr);
    }

    //grid配置
    var grid = Ext.create('Ext.grid.Panel', {
        id: "Sys_RolePermissionPanel",
        renderTo: "Sys_RolePermissionGrid",
        layout: "fit",
        alias: 'widget.methodPanel',
        columnLines: true,
        loadMask: true,
        height: 500,
        frame: true,
        border: false,
        autoScroll: true,
        store: Sys_RolePermissionStore,
        ds: Sys_RolePermissionStore,
        columnLines: true,
        features: [groupingFeature],
        //cm: cm2,
        viewConfig: {
            stripeRows: true,
            trackOver: true,
            emptyText: '<div style="color:#999;margin:5px;">当前没有记录显示<div>'
        },
        stripeRows: true,
        listeners: {
            "itemdblclick": function(g, rowIndex, e) {
                EditSys_RolePermissionFn();
            }
        },
        tbar: [            		 //工具条设置
            		 {
            		 text: "保存",
            		 tooltip: "保存",
            		 iconCls: "addicon",
            		 handler: SaveSys_RolePermissionFn
           }, {
               text: "全选/全清",
               tooltip: "全选/全清",
               // iconCls: "addicon",
               handler: CheckSys_RolePermissionFn
}],
        columns: [
               Ext.create('Ext.grid.RowNumberer'),
                { header: "模块", dataIndex: "M_CName", width: 80, sortable: true },
                { header: "模块编码", dataIndex: "M_PageCode", width: 80, sortable: true },
                { header: "权限列表", dataIndex: "M_Icon", width: 440, sortable: false }

            ]
    });
    Sys_RolePermissionStore.loadPage(1);
   
}
    Sys_RolePermissionList();
